package com.proactive.webapp.controller;

import java.io.File;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.proactive.webapp.dao.ActivityImageDAO;
import com.proactive.webapp.entity.Activity;
import com.proactive.webapp.entity.ActivityImage;
import com.proactive.webapp.entity.Manager;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.util.DateConversion;
import com.proactive.webapp.util.FileUtil;
@Controller
public class ActivityImageController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");
	
	@RequestMapping(value = "/management/addActivityImage", method = RequestMethod.POST)
	public ModelAndView addActivityImage(ActivityImage activityImage, HttpSession session){
		ModelAndView model = new ModelAndView();
		Manager loginManager = (Manager)session.getAttribute("loginManager");
		if(loginManager != null){
			ActivityImageDAO activityImageDAO = (ActivityImageDAO)context.getBean("activityImageDAO");
			String path = session.getServletContext().getRealPath("resources/image/activity/carousel");
			
			DateConversion dateConversion = new DateConversion();
			Date now = new Date();
			
			if(activityImage.getImage().getSize() > 0){
				FileUtil fileUtil = new FileUtil(activityImage.getImage().getOriginalFilename());
				String newFileName = dateConversion.ConversionYMDHMS2(now) + "." + fileUtil.getFileType();
				File targetFile = new File(path, newFileName);
		        if(!targetFile.exists()){
		            targetFile.mkdirs();
		        }
		        try {
		        	activityImage.getImage().transferTo(targetFile);
		        } catch (Exception e) {
		            e.printStackTrace();
		        }
		        activityImage.setImagePath("resources/image/activity/carousel/"+newFileName);
		   
			}
			
			activityImageDAO.insert(activityImage);
			model.setViewName("redirect:/management/modiActivityCont");
		}
		else
			model.setViewName("redirect:/management/login");
		return model;
	}
	
	@RequestMapping(value = "/management/delActivityImage", method = RequestMethod.POST)
	public ModelAndView delActivityImage(HttpServletRequest request, HttpSession session) {
		ModelAndView model = new ModelAndView();
		Manager loginManager = (Manager)session.getAttribute("loginManager");
		if(loginManager != null){
			ActivityImageDAO activityImageDAO = (ActivityImageDAO)context.getBean("activityImageDAO");
			ActivityImage activityImage = new ActivityImage();
			String id = request.getParameter("id");
			String path = session.getServletContext().getRealPath("/");
			
			activityImage.setId(Integer.parseInt(id));
			activityImageDAO.get(activityImage);
			activityImageDAO.delete(activityImage);
			
			if(!activityImage.getImagePath().equals("")){
				File file = new File(path+activityImage.getImagePath());
				file.delete();
			}
			
			model.setViewName("redirect:/management/modiActivityCont");
		}
		else
			model.setViewName("redirect:/management/login");
		
		return model;
	}

}
